package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;




public class FtpFileSender {
	private String domain="";
	private String user="";
	private String password="";
	private File fileToSend=new File("");
	private String outputPath ="";
	
	private static Logger logger = LogManager.getLogger(FtpFileSender.class);
	
	public String getDomain() {
		return domain;
	}
	public void setDomain(String domain) {
		this.domain = domain;
	}
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public File getFileToSend() {
		return fileToSend;
	}
	public void setFileToSend(File fileToSend) {
		this.fileToSend = fileToSend;
	}
	public FtpFileSender(String domain, String user, String password,
			File fileToSend,String outputPath) {
		super();
		this.domain = domain;
		this.user = user;
		this.password = password;
		this.fileToSend = fileToSend;
		this.outputPath = outputPath;
	}
	public boolean sendFile(String folder){
		boolean returnValue=true;
		
		FTPClient client = new FTPClient();
	    FileInputStream fis = null;
	    
		try {
	        client.connect(domain);
	        
	        client.setFileType(FTP.BINARY_FILE_TYPE);
	        
	        client.setDefaultTimeout(30000);
	     
	        client.login(user, password);

	        fis = new FileInputStream(fileToSend.getPath());
	        
	        client.changeWorkingDirectory(outputPath+"/"+folder);
	        
	        returnValue = client.storeFile(fileToSend.getName(), fis);
	        
	        logger.debug(client.getReplyString());
	        
	        fis.close();
	        client.logout();
	        
	    } catch (IOException e) {
	       logger.error("",e);
	       returnValue=false;
	    }
		
		return returnValue;
	}
	public boolean checkFolder(String folder){
		boolean returnValue=true;
		
		FTPClient client = new FTPClient();
	    FileInputStream fis = null;
	    
		try {
			client.setDefaultTimeout(5000);
			client.setDefaultPort(21);
//			client.enterLocalPassiveMode();
			
	        client.connect(domain);
	        
	        int replyCode=client.getReplyCode();
	        
	        if(FTPReply.isPositiveCompletion(replyCode)) {
	        	client.login(user, password);
	        	
	        	
	        	logger.debug(""+client.getReplyString());
	        	
	        	returnValue= client.changeWorkingDirectory(outputPath+"/"+folder);
	        	
	        	logger.debug("Current directory is " + client.printWorkingDirectory());
	        	
	        	logger.debug(client.getReplyString());

	        }
	        	
	        
	        client.logout();
	        
	    } catch (IOException e) {
	       logger.error("",e);
	       returnValue=false;
	    }
		
		return returnValue;
	}
	public boolean createFolder(String folder){
		boolean returnValue=true;
		
		FTPClient client = new FTPClient();
	    FileInputStream fis = null;
	    logger.debug(outputPath);
		try {
			client.setDefaultTimeout(5000);
			client.setDefaultPort(21);
//			client.enterLocalPassiveMode();
			
	        client.connect(domain);
	        
	        int replyCode=client.getReplyCode();
	        
	        if(FTPReply.isPositiveCompletion(replyCode)) {
	        	client.login(user, password);
	        	
	        	
	        	logger.debug(""+client.getReplyString());
	        	
	        	client.changeWorkingDirectory(outputPath);
	        	
	        	logger.debug("Current directory is " + client.printWorkingDirectory());
	        	
	        	returnValue= client.makeDirectory(folder);
	        	logger.debug(client.getReplyString());
	        	
	        }
	        client.logout();
	        
	    } catch (IOException e) {
	       logger.error("",e);
	       returnValue=false;
	    }
		
		return returnValue;
	}
}
